% file estima_fcastroll_out
numpar_est = length(parposest); 
nc = 5; 
nr = ceil(numpar_est/nc);
parevolve_mat_est = parevolve_mat(parposest, :); 
parnames_est = parnames(parposest); 

ghand = figure; 
for ii = 1:numpar_est
    subplot(nr, nc, ii);
    plot((firstdest:0.25:lastdest), parevolve_mat_est(ii, :),'LineWidth',1.5);
    title(parnames_est(ii)); 
    vline(2008, 'r'); 
    set(gca,'XTickLabel','')
end 
suptitle('rolling estimates (red line: 2008q1 )'); 
paper_orientation = 'Landscape';
paper_position = [0.2 0.1 10.5 8.3];
set(ghand,'PaperOrientation', paper_orientation);
set(ghand,'PaperPosition',paper_position);

cd(outpath); 
print(ghand, '-dpdf', 'parevolve');
cd(cucd); 

%%
% forecast vs. true data

nforc = 16; 
allforc_dsge = zeros(1+nforc, nz, nroll); 
allferr_dsge = zeros(1+nforc, nz, nroll); 

allforc_var1 = zeros(1+nforc, nz, nroll); 
allferr_var1 = zeros(1+nforc, nz, nroll); 

allforc_var2 = zeros(1+nforc, nz, nroll); 
allferr_var2 = zeros(1+nforc, nz, nroll); 

%% Generating Forecasts and Compute Forecast Errors
for ii = 1:nroll;
    
   dest = firstdest + 0.25*(ii - 1);
   dest_str = sample2date(dest, '');
   dest_str = dest_str{:}; 
   disp('')
   disp('____________________________________')
   disp(['forecasting at date ', dest_str ]); 
    
    nper_est = find(dest == sample);
    Y = Yfull(1:nper_est, :);
    
    if 17 >= nper_est;
        error('not enough periods');
    end
    trainvec = [17, nper_est];
    
    parvecmode = parevolve_mat(:, ii); 
    
    outforc=forc_dsge(Yfull(1:min(trainvec(2)+nforc, length(Yfull)), :),trainvec,funcmod,parvecmode,solveopt,addsol,nforc);  
    
    allforc_dsge(:, :, ii) = outforc.forc_dsge; 
    allferr_dsge(:, :, ii) = outforc.rmse_dsge; 
  
    allforc_var1(:, :, ii) = outforc.forc_var1; 
    allferr_var1(:, :, ii) = outforc.rmse_var1; 
    
    allforc_var2(:, :, ii) = outforc.forc_var2; 
    allferr_var2(:, :, ii) = outforc.rmse_var2; 
    
    
    cd(outpath); 
    eval(['save ', savefname, ' -append']); 
    cd(cucd); 
end
% sum of squared error
sse_dsge = sum(nan2nil(allferr_dsge).^2, 3); 
sse_var1 = sum(nan2nil(allferr_var1).^2, 3); 
sse_var2 = sum(nan2nil(allferr_var2).^2, 3);

sse_dsge = sse_dsge(2:end, :); 
sse_var1 = sse_var1(2:end, :); 
sse_var2 = sse_var2(2:end, :); 

Tvec = nroll*ones(nforc, 1) - (1:nforc)'; 
Tmat = repmat(Tvec, [1, nz]); 


rmse_dsge = (sse_dsge./Tmat).^0.5; 
rmse_var1 = (sse_var1./Tmat).^0.5; 
rmse_var2 = (sse_var2./Tmat).^0.5; 


rrmse_dsge_to_var2 = rmse_dsge./rmse_var2; 
rrmse_var1_to_var2 = rmse_var1./rmse_var2; 

cd(outpath); 
save fcastres rmse_dsge rmse_var1 rmse_var2 rrmse_dsge_to_var2 rrmse_var1_to_var2
cd(cucd); 


cd(outpath); 
eval(['save ', savefname, ' -append']); 
cd(cucd); 


%%

pos_start = find(sample == firstdest); 
pos_end = find(sample == lastdest); 
Ytrue = Y(pos_start:pos_end, :); 


hor_vec = [1, 4, 8, 12, 16]; 
numhor = length(hor_vec); 
for ii = 1:numhor
hor = hor_vec(ii);
ghand = figure; 
gvec(ii) = ghand;
for var = 1:nz;
    % var = 4;
    data_vec = Ytrue(1 + hor:end, var);
    
    fcast_dsge = squeeze(allforc_dsge(hor+1, var, : ));
    fcast_dsge = fcast_dsge(1:Tvec(hor));
    
    fcast_vec_var1 = squeeze(allforc_var1(hor+1, var, : ));
    fcast_vec_var1 = fcast_vec_var1(1:Tvec(hor));
    
    fcast_vec_var2 = squeeze(allforc_var2(hor+1, var, : ));
    fcast_vec_var2 = fcast_vec_var2(1:Tvec(hor));
    
    if length(data_vec) ~= length(fcast_dsge) && var == 1;
        disp('')
        disp('__________')
        disp(['hor = ', num2str(hor)])
        disp(['len data = ', num2str(length(data_vec))]); 
        disp(['len dsge = ', num2str(length(fcast_dsge))]); 
        disp(['len var(1) = ', num2str(length(fcast_vec_var1))]); 
        disp(['len var(2) = ', num2str(length(fcast_vec_var2))]); 
        %error('check length of the data plotted')
    end 
    
    subplot(nz, 3, (var - 1)*3+1);
    
    plot(data_vec, 'Color', 'k', 'LineWidth',1.5); hold on
    plot(fcast_dsge,'Color', 'r', 'LineWidth',1.5);
    if var == 1; title('DSGE 1'); end
    ylabel(Ynames(var))
     
    subplot(nz, 3, (var-1)*3 + 2);
    plot(data_vec, 'Color', 'k', 'LineWidth',1.5); hold on
    plot(fcast_vec_var1, 'Color','r', 'LineWidth',1.5);
    if var == 1; title('VAR(1)'); end
    
    subplot(nz, 3, (var-1)*3 + 3);
    plot(data_vec, 'Color', 'k', 'LineWidth',1.5); hold on
    plot(fcast_vec_var2,'Color',  'r', 'LineWidth',1.5);
    if var == 1; title('VAR(2)'); end
end
    suptitle([num2str(hor), ' period(s) ahead forecasts vs. actual data' ]);
    subtitulo(outpath)
paper_orientation = 'Landscape';
paper_position = [0.2 0.1 10.5 8.3];
set(ghand,'PaperOrientation', paper_orientation);
set(ghand,'PaperPosition',paper_position);
end

save_multipages(gvec, 'fcast_vs_data', outpath); 
close all; 

%%


hor_sel = [1, 2, 3, 4, 8, 12, 16]; 
numhor_sel = length(hor_sel); 


% notice rows: horizons
%        cols: obs. 
rmse_var2_base = rmse_var2'; 

rela_rmse_var1 = (rmse_var1./rmse_var2)'; 
rela_rmse_dsge = (rmse_dsge./rmse_var2)';
% rela_rmse_dsge2 = (rmse_dsge2./rmse_var2)';


outcell = {'Model','1Q', '2Q', '3Q', '4Q', '8Q', '12Q', '16Q'}; 
endcell = [{'N. Obs.'}, num2cprec(Tvec(hor_sel)')]; 
notecell = {'RMSE Window', '2000q1', 'to', '2009q4'}; 
outcell = merge_cells(notecell, outcell, 1); 

vernames = {'VAR(2)'; 'Relative RMSE'; 'VAR(1)'; 'DSGE'}; 
for ii = 1:nz;
    tempvar_mat = zeros(2, numhor_sel); 
    tempvar_mat(1, :) = rela_rmse_var1(ii, hor_sel);
    tempvar_mat(2, :) = rela_rmse_dsge(ii, hor_sel);
    % tempvar_mat(3, :) = rela_rmse_dsge2(ii, hor_sel);
    tempvar_cell = num2cprec(tempvar_mat, 3); 
    tempvar_cell = merge_cells(num2cprec(rmse_var2_base(ii, hor_sel), 3), tempvar_cell, 1); 
    tempvar_cell = [vernames, tempvar_cell]; 
    tempvar_cell = merge_cells(Ynames(ii), tempvar_cell); 
    outcell = merge_cells(outcell, tempvar_cell, 1); 
end 

outcell = merge_cells(outcell, endcell, 1); 

cd(outpath); 
xlswrite('rmse_compare', outcell); 
cd(cucd)





